home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / compress.zoo / compress.c < prev    next >
C/C++ Source or Header  |  1989-06-30  |  26KB  |  621 lines

  1. /*@H************************ < COMPRESS utility> ****************************
  2. *                                                                           *
  3. *   compress : compress.c                                                   *
  4. *              Main and Operating System Independent support functions      *
  5. *                                                                           *
  6. *   port by  : Donald J. Gloistein                                          *
  7. *                                                                           *
  8. *   Source, Documentation, Object Code:                                     *
  9. *   released to Public Domain. This code is ported from compress v4.0       *
  10. *   release joe.                                                            *
  11. *---------------------------  Module Description  --------------------------*
  12. *   The compress program is compatible with the compression/decompression   *
  13. *   used on the Unix systems compress programs.  This is version 4 and      *
  14. *   supports up to 16 bits compression. The porting retained the Unix       *
  15. *   meanings of all options, added a couple for MsDos and modified the      *
  16. *   file name conventions to make more sense.                               *
  17. *                                                                           *
  18. *--------------------------- Implementation Notes --------------------------*
  19. *                                                                           *
  20. *   compiled with : compress.h compress.fns                                 *
  21. *   linked with   : compapi.obj  compusi.obj                                *
  22. *   problems:                                                               *
  23. *              See notes in compress.h for defines needed.                  *
  24. *              It should work now with Xenix                                *
  25. *                                                                           *
  26. *              Check the signal() handler functions in your compiler        *
  27. *              documentation. This code assumes ANSI SYS V compatible       *
  28. *              header and return values. Change as appropriate for your     *
  29. *              compiler and operating system.                               *
  30. *                                                                           *
  31. *              This source compiles properly with Microsoft C compiler      *
  32. *              version 5.1.                                                 *
  33. *                                                                           *
  34. *   CAUTION:   because the program is in modules, make sure you recompile   *
  35. *              all modules if you change the header or a define in the      *
  36. *              compress.c file                                              *
  37. *                                                                           *
  38. * Algorithm from "A Technique for High Performance Data Compression",       *
  39. * Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19.          *
  40. *                                                                           *
  41. * Assumptions:                                                              *
  42. *  When filenames are given, replaces with the compressed version           *
  43. *  (.Z suffix) only if the file decreases in size.                          *
  44. * Algorithm:                                                                *
  45. *  Modified Lempel-Ziv method (LZW).  Basically finds common                *
  46. * substrings and replaces them with a variable size code.  This is          *
  47. * deterministic, and can be done on the fly.  Thus, the decompression       *
  48. * procedure needs no input table, but tracks the way the table was built.   *
  49. *                                                                           *
  50. *                                                                           *
  51. *---------------------------      Author(s)        -------------------------*
  52. *     Initials ---- Name ---------------------------------                  *
  53. *      DjG          Donald J. Gloistein                                     *
  54. *                   Plus many others, see rev.hst file for full list        *
  55. *      LvR          Lyle V. Rains, many thanks for improved implementation  *
  56. *************************************************************************@H*/
  57.  
  58. /*@R************************< Revision History >*****************************
  59. *                                                                           *
  60. *   version -- date -- init ---Notes----------------------                  *
  61. *    4.01    08-29-88  DjG    first cut  for 16 bit MsDos version           *
  62. *            09-04-88  DjG    fixed unlink on zcat if interupted.           *
  63. *                             added msdos filename logic and functions      *
  64. *    4.10    10-27-88  DjG  revised API with coding changes by LvR.         *
  65. *    4.10a   10-30-88  DjG  cleaned up code and fixed bug in freeing ptr.   *
  66. *    4.10b   11-01-88  DjG  cleaned up the logic for inpath/outpath         *
  67. *                           Changed the logic to finding the file name      *
  68. *                           Fixed the allocation bug in the api             *
  69. *                           Added some more portability macros              *
  70. *    4.10c   11-04-88  DjG  Changed maxcode from global to static in api.   *
  71. *                           Supplied some library functions for those who   *
  72. *                           don't have them, changed dos usi to use the     *
  73. *                           strrpbrk(). Checked casts in api again. Compiles*
  74. *                           without warnings at pick level 3.               *
  75. *    4.10d   11-25-88  DjG  revised some memory allocation, put more in the *
  76. *                           header file. Corrected some typos.              *
  77. *                           Changed prog_name() to force lower case         *
  78. *                           Corrected bug, no longer unlinks existing file  *
  79. *                           if not enough memory to compress or decompress  *
  80. *            12-06-88  DjG  VERY minor changes for casts and header defines *
  81. *            12-08-88  DjG  Adjusted path separator check in main function  *
  82. *                           Amiga uses split seg because of compiler        *
  83. *            12-09-88  DjG  Debugging done, all defaults now Unix compress  *
  84. *                           defaults, including unlinking input file and    *
  85. *                           acting as a filter. Must use -h option to get   *
  86. *                           help screen.                                    *
  87. *    4.10e   12-11-88  DjG  Fixed more casts, prototypes and header file.   *
  88. *    4.10f   12-12-88  DjG  Fixed unlinking open files on error. This fails *
  89. *                           on shared or os/2 platforms.                    *
  90. *            12-15-88  DjG  Fixed SIGTYPE for function passed to signal     *
  91. *                           Fixed problems with Xenix 2.2.1                 *
  92. *    4.2     12-19-88  DjG  Replaced adaptive reset as an option.           *
  93. *    4.3     12-26-88  DjG  Fixed long file name bug, fixed bug with        *
  94. *                           compressdir. -B option added, same as -b option *
  95. *            05-06-89  Dal  Ported to Sozobon/Alcyon C for Atari ST.  Also, *
  96. *                           created get_one() for console prompting.        *
  97. *            05-08-89  Dal  Ported to Minix-ST                              *
  98. *************************************************************************@R*/
  99.  
  100. #include <stdio.h>
  101.  
  102. #define MAIN        /* header has defining instances of globals */
  103. #include "compress.h" /* contains the rest of the include file declarations */
  104.  
  105. #define ARGVAL() (*++(*argv) || (--argc && *++argv))
  106. char suffix[] = SUFFIX ;          /* only used in this file */
  107.  
  108. void main( argc, argv )
  109. register int argc; char **argv;
  110. {
  111.     char **filelist, **fileptr,*temp;
  112.     struct stat statbuf;
  113.  
  114. #ifndef NOSIGNAL
  115.     if ( (bgnd_flag = signal ( SIGINT, SIG_IGN )) != SIG_IGN ) {
  116.         /* ANSI/SYS V compatible */
  117.         /* the following test checks for error on setting signals */
  118.         /* check your documenta